import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.renderer.xy.*;
import org.jfree.ui.ApplicationFrame;

public class CombinedGraph extends ApplicationFrame
{
	private static final long serialVersionUID = 1L;

	public CombinedGraph ( double velocity, int displacement, double [] constants, double[][] phase, double[][] currentVelocity, double[][] targetVelocity, double[][] pidOutput, double[][] currentDisplacement, double[][] motorOutput )
	{
		super("PID Output");
		
		// Creates x and y axis
		NumberAxis domainAxis = new NumberAxis( "Time" );
		domainAxis.setAutoRangeIncludesZero(  true  );
		NumberAxis rangeAxis = new NumberAxis( "PWM" );
		rangeAxis.setAutoRangeIncludesZero(  true );
		
		// Creates overall graph/plot
		CombinedDomainXYPlot graph = new CombinedDomainXYPlot( domainAxis );
		
		// Creates XYSeries
		XYSeries d_phase = new XYSeries( "Phase" );
		XYSeries d_targetVelocity = new XYSeries( "Target Velocity" );
		XYSeries d_currentVelocity = new XYSeries( "Current Velocity" );
		XYSeries d_pidOutput = new XYSeries( "PID Output" );
		XYSeries d_motorOutput = new XYSeries( "Motor Output" );
		for ( int i=0; i<targetVelocity[0].length; i++ )
		{
			d_phase.add( phase[0][i], phase[1][i] );
			d_targetVelocity.add( targetVelocity[0][i], targetVelocity[1][i] );
			d_currentVelocity.add( currentVelocity[0][i], currentVelocity[1][i] );
			d_pidOutput.add( pidOutput[0][i], pidOutput[1][i] );
			d_motorOutput.add( motorOutput[0][i], motorOutput[1][i] );
		}
		
		// Adds all XYSeries of a XYSeriesCollection
		XYSeriesCollection d_collection = new XYSeriesCollection ();
		d_collection.addSeries( d_phase );
		d_collection.addSeries( d_targetVelocity );
		//d_collection.addSeries( d_currentVelocity );
		//d_collection.addSeries( d_pidOutput );
		d_collection.addSeries( d_motorOutput );
		
		
		JFreeChart chart = ChartFactory.createXYLineChart( "PID", "Time", "PWM", d_collection, PlotOrientation.VERTICAL, true, true, false );
		chart.setBackgroundPaint(Color.white);
		XYPlot plot1 = chart.getXYPlot();
		plot1.setBackgroundPaint(Color.white);
		plot1.setDomainGridlinePaint(Color.lightGray);
		plot1.setRangeGridlinePaint(Color.lightGray);
		XYPlot plot2 = chart.getXYPlot();
		plot2.setBackgroundPaint(Color.white);
		plot2.setDomainGridlinePaint(Color.lightGray);
		plot2.setRangeGridlinePaint(Color.lightGray);
		XYPlot plot3 = chart.getXYPlot();
		plot3.setBackgroundPaint(Color.white);
		plot3.setDomainGridlinePaint(Color.lightGray);
		plot3.setRangeGridlinePaint(Color.lightGray);
		
		
		// Adds chart onto panel
		ChartPanel panel = new ChartPanel ( chart, true );
		panel.setPreferredSize(new java.awt.Dimension(2560, 1440));
        panel.restoreAutoBounds();

        setContentPane(panel);
		
		
	}
}
